/**
 * 能量商店
 */
class energyStore extends eui.Component{
    private return:eui.Button;
    private energyNum:eui.Label; 
    private energyList:eui.List;
    private _listData:Array<any> = [];

    private recordList:eui.List;
    private _arrayCollection:eui.ArrayCollection;
    
    public constructor(){
        super();
       // this.cacheAsBitmap =true;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "energyStoreSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.inIt();
    }

    private inIt(){
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.dispatchEventWith("return");
        },this);

        Dispatcher.ins.addEventListener("shopList",this.setList,this);
        Dispatcher.ins.addEventListener("createOrder",this.close,this);
        Dispatcher.ins.addEventListener("recordList",this.setRecordList,this);
        Dispatcher.ins.addEventListener("buyEnergySuccess",this.success,this);

        this.energyList.itemRenderer = energyIR;
        this.energyList.addEventListener( eui.ItemTapEvent.ITEM_TAP,(evt:GameEvent)=>{
            var index = evt.target.selectedIndex;
            
            let orderList:any = [];
            let obj = this._listData[index];
            let trObj:any = {};
            trObj.propId = obj.Id;
            trObj.propName = obj.PropName;
            trObj.propNum = 1;
            trObj.price = obj.Price;
            trObj.parPrice = obj.ParPrice;
            trObj.sType = obj.SType;
            orderList.push(trObj);
                
            let dataPost = {
                "userId":common.userInfo.userId,
                "areaId":common.areaId,
                "gameId":common.gameId,
                "chnltype":common.chnltype,
                "numId":common.userInfo.numId,
                "orderList":orderList,
                "version":common.version
            }
            http.getHttpServer("createOrder",dataPost,"energy");
        },this);

        this.getList();
    }
    public getList():void{
        let dataPost:any = {"sType":3};
        socket.request("shopList",dataPost,3);
    }
    private setList(evt:GameEvent):void{
        let obj = evt.data;
        if(obj.note !== 3) return;
        this._listData = obj.list;
        this.getRecordList();
    }
    
    private close(evt:GameEvent):void{
        let obj = evt.data;
        if(obj.note == "energy"){
            this.wxPay(obj.data);
        }
    }
    private wxPay(data):void{
        if(wx) {
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: data.appId, // 必填，公众号的唯一标识
                timestamp: data.timeStamp, // 必填，生成签名的时间戳
                nonceStr: data.nonceStr, // 必填，生成签名的随机串
                signature: data.sign,// 必填，签名
                jsApiList: ["chooseWXPay"] // 必填，需要使用的JS接口列表
            });
            wx.ready(function() {
                // alert('ready');
                wx.chooseWXPay({
                    timestamp: data.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                    nonceStr: data.nonceStr, // 支付签名随机串，不长于 32 位
                    package: data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                    signType: data.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                    paySign: data.sign, // 支付签名
                    success: function (res) {
                        // 支付成功后的回调函数
                        // alert('success');
                        if(res.errMsg == "chooseWXPay:ok"){
                            Dispatcher.ins.dispatchEvent(new GameEvent("buyEnergySuccess"));
                        }
                    }
                });
            });
            wx.error(function(res){
                // alert(JSON.stringify(res));
                // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
            });
        }
    }
    public getRecordList():void{
        this.energyNum.text = common.userInfo.energy + "";
        //获取购买记录
        let dataPost = {"sType":3}
        socket.request("recordList",dataPost);
    }
    private setRecordList(evt:GameEvent):void{
        let obj = evt.data;
        let list = obj.list;
        let arr = [];
        for(let i = 0,lg = list.length;i < lg;i++){
            let obj = list[i];
            let trArr = obj.LogTime.split(" ");
            obj.month = trArr[0];
            obj.time = trArr[1];
            arr.push(obj);
        }
        this._arrayCollection = new eui.ArrayCollection(arr);
        this.recordList.dataProvider = this._arrayCollection;
        this._arrayCollection.refresh();
    }
    private success():void{
        socket.request("getEnergy");
        this.dispatchEventWith("return");
    }
}

class energyIR extends eui.ItemRenderer{
    private click:eui.Image;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "energyIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
            this.click.visible = true;
        },this);
        this.addEventListener(egret.TouchEvent.TOUCH_END,()=>{
            this.click.visible = false;
        },this);
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{
            this.click.visible = false;
        },this);
    }
    protected dataChanged():void{

    }
}